import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

import 'GestureDetectorConflictTest.dart';
import 'GestureDetectorHitTest.dart';
import 'NotificationInterruptTest.dart';
import '../function/change_notifier_use.dart';

class GestureTestRout extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Material(
      child: SingleChildScrollView(
        child: Column(
          children: [
            TextButton(
              child: Text("跳转自定义拖拽手势位置监听，小球移动"),
              onPressed: () {
                var result = Navigator.push(context,
                    MaterialPageRoute(builder: (context) {
                  return _Drag();
                }));
              },
            ),

            TextButton(
              child: Text("手势缩放"),
              onPressed: () {
                var result = Navigator.push(context,
                    MaterialPageRoute(builder: (context) {
                  return _Scale();
                }));
              },
            ),


            TextButton(
              child: Text("Listener控件的命中测试"),
              onPressed: () {
                var result = Navigator.push(context,
                    MaterialPageRoute(builder: (context) {
                  return PointerDownListenerRoute();
                }));
              },
            ),

            TextButton(
              child: Text("覆盖控件 Listener控件的命中测试"),
              onPressed: () {
                var result = Navigator.push(context,
                    MaterialPageRoute(builder: (context) {
                  return WaterMaskTest();
                }));
              },
            ),

            TextButton(
              child: Text("手势冲突处理-只有一个响应 测试"),
              onPressed: () {
                var result = Navigator.push(context,
                    MaterialPageRoute(builder: (context) {
                  return GestureDetectorConflictTest();
                }));
              },
            ),

            TextButton(
              child: Text("手势冲突处理-只有一个响应 测试---但是使用自定义的手势处理器导致，都能响应"),
              onPressed: () {
                var result = Navigator.push(context,
                    MaterialPageRoute(builder: (context) {
                  return CustomGestureDetectorConflictTest();
                }));
              },
            ),

            TextButton(
              child: Text("竖直水平方向竞争 测试"),
              onPressed: () {
                var result = Navigator.push(context,
                    MaterialPageRoute(builder: (context) {
                  return BothDirectionTest();
                }));
              },
            ),

            TextButton(
              child: Text("水平方向拖拽，监听手指按下，抬起，拖拽结束 测试"),
              onPressed: () {
                var result = Navigator.push(context,
                    MaterialPageRoute(builder: (context) {
                  return GestureConflictTestRoute();
                }));
              },
            ),

            TextButton(
              child: Text("自定义通知 Notification  自下而上 拦截"),
              onPressed: () {
                var result = Navigator.push(context,
                    MaterialPageRoute(builder: (context) {
                  return NotificationRoute();
                }));
              },
            ),


          ],
        ),
      ),
    );
  }
}

///小球拖拽移动的控件
class _Drag extends StatefulWidget {
  @override
  _DragState createState() => _DragState();
}

class _DragState extends State<_Drag> with SingleTickerProviderStateMixin {
  double _top = 0.0; //距顶部的偏移
  double _left = 0.0; //距左边的偏移

  @override
  Widget build(BuildContext context) {
    return Container(
        color: Color(0xFF1499AA),
        child: LayoutBuilder(
            builder: (BuildContext context, BoxConstraints constraints) {
          return Stack(
            children: <Widget>[
              Positioned(
                width: 100,
                height: 100,
                top: _top,
                left: _left,
                child: GestureDetector(
                  child: CircleAvatar(child: Text("A")),
                  //手指按下时会触发此回调
                  onPanDown: (DragDownDetails e) {
                    //打印手指按下的位置(相对于屏幕)
                    print("用户手指按下：${e.globalPosition}");
                  },
                  //手指滑动时会触发此回调
                  onPanUpdate: (DragUpdateDetails e) {
                    //用户手指滑动时，更新偏移，重新构建
                    setState(() {
                      var _leftX = _left + e.delta.dx;
                      var _topY = _top + e.delta.dy;
                      if (_leftX >= 0 &&
                          _topY > 0 &&
                          _leftX <= constraints.maxWidth - 100 &&
                          _topY <= constraints.maxHeight - 100) {
                        _left = _leftX;
                        _top = _topY;
                      }
                      print("移动后的位置 left：${this._left}--top:$_top");
                      print("BoxConstraints：${context.widget}");
                      print(
                          " context.size!.height：${context.size!.height}-- context.size!.width :${context.size!.width}");
                    });
                  },
                  onPanEnd: (DragEndDetails e) {
                    //打印滑动结束时在x、y轴上的速度
                    print(e.velocity);
                  },
                ),
              )
            ],
          );
        }));
  }
}


///手势缩放的控件
class _Scale extends StatefulWidget {
  const _Scale({Key? key}) : super(key: key);

  @override
  _ScaleState createState() => _ScaleState();
}

class _ScaleState extends State<_Scale> {
  double _width = 200.0; //通过修改图片宽度来达到缩放效果
  var imageUrl = "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.jj20.com%2Fup%2Fallimg%2Ftp05%2F1Z9291TIBZ6-0-lp.jpg&refer=http%3A%2F%2Fimg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1644809008&t=8bf2bd00ec0da949763c644935e7c0cd";
  @override
  Widget build(BuildContext context) {
    return Center(
      child: GestureDetector(
        //指定宽度，高度自适应
        child: Image.network(
          imageUrl,
        ),
        onScaleUpdate: (ScaleUpdateDetails details) {
          print("缩放监听");
          setState(() {
            //缩放倍数在0.8到10倍之间
            _width=200*details.scale.clamp(.8, 10.0);
          });
        },
      ),
    );
  }
}